<script setup>
import {
  Search,
  Refresh,
  Plus,
  Delete,
  Download,
  Edit
} from '@element-plus/icons-vue'
import diaLog from '@/views/admin/components/vegetableplotdialog.vue'
import { ref } from 'vue'
import { utils, writeFile } from 'xlsx'
import { vegetableinfoGetService } from '@/api/admin/vegetableplot'
const loading = ref(false)
const dialog = ref()
const vegetableinfo = ref([])
const vegetableinfoview = ref([])
//获取数据库数据
const getLandparcelreInfoList = async () => {
  loading.value = true
  const res = await vegetableinfoGetService()
  vegetableinfo.value = res.data.data
  vegetableinfoview.value = res.data.data
  loading.value = false
}
getLandparcelreInfoList()
//重新渲染
const onSuccess = () => {
  getLandparcelreInfoList()
}
//添加操作
const addLandreinfo = () => {
  dialog.value.open({})
}
//编辑操作
const onEditUserinfo = (row) => {
  dialog.value.open(row)
}
//删除
import { vegetableplotinfoDlService } from '@/api/admin/vegetableplot'
const onDelVegetablePlotinfo = async (row) => {
  await ElMessageBox.confirm('是否删除该记录?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
  vegetableplotinfoDlService(row.id, row.landparcelid, row.status)
    .then((response) => {
      if (response.data.status == 200) {
        ElMessage.success(response.data.message)
        getLandparcelreInfoList()
      } else {
        ElMessage.error(response.data.message)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
//搜索
const keyname = ref('')
const keyaddress = ref('')
const keystatus = ref('')
const keySearch = () => {
  vegetableinfoview.value = []
  vegetableinfoview.value = vegetableinfo.value.filter((row) => {
    if (
      (keyaddress.value === '' || row.address.includes(keyaddress.value)) &&
      (keyname.value === '' || row.username.includes(keyname.value)) &&
      (keystatus.value === '' || row.status.includes(keystatus.value))
    ) {
      return true // 符合搜索条件的保留
    } else {
      return false // 不符合搜索条件的过滤掉
    }
  })
}
//重置搜索条件
const resetFilter = () => {
  vegetableinfoview.value = vegetableinfo.value
  keyname.value = ''
  keyaddress.value = ''
  keystatus.value = ''
}
//导出数据
const table = ref(null)
const exportExcel = () => {
  if (table.value) {
    const sheet = utils.table_to_sheet(table.value.$el)
    const workbook = utils.book_new()
    utils.book_append_sheet(workbook, sheet, 'Sheet1')
    writeFile(workbook, 'vegetableinfo_data.xlsx')
  }
}
</script>

<template>
  <page-container title="用户信息">
    <!-- 头部 -->
    <template #extra>
      <strong>所在地</strong>
      <el-input
        placeholder="输入关键字"
        style="width: 150px"
        v-model="keyaddress"
      /><strong>种植户</strong>
      <el-input
        placeholder="输入关键字"
        style="width: 100px"
        v-model="keyname"
      /><strong>完成状态</strong>
      <el-select v-model="keystatus" placeholder="请选择" style="width: 100px">
        <el-option label="是" value="是"></el-option>
        <el-option label="否" value="否"></el-option>
      </el-select>

      <el-button :icon="Search" @click="keySearch">搜索</el-button>
      <el-button :icon="Refresh" @click="resetFilter">重置</el-button>
      <el-button type="primary" plain :icon="Plus" @click="addLandreinfo"
        >新增</el-button
      >

      <el-button type="warning" plain :icon="Download" @click="exportExcel"
        >导出</el-button
      >
    </template>
    <el-table
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      ref="table"
      :data="vegetableinfoview"
    >
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="name" label="菜地名称" width="150" />
      <el-table-column prop="crops" label="种植作物" width="150" />
      <el-table-column prop="landresourcename" label="土地资源名称" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{
            scope.row.landresourcename
          }}</span>
        </template></el-table-column
      >
      <el-table-column prop="address" label="土地资源所在地" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{
            scope.row.address
          }}</span>
        </template></el-table-column
      >
      <el-table-column prop="order" label="土地块编号" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{ scope.row.order }}</span>
        </template></el-table-column
      >
      <el-table-column prop="area" label="种植面积(亩)" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{ scope.row.area }}</span>
        </template></el-table-column
      >
      <el-table-column prop="ph" label="PH" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{ scope.row.ph }}</span>
        </template></el-table-column
      >
      <el-table-column prop="starttime" label="种植开始时间" width="150" />
      <el-table-column prop="endtime" label="种植结束时间" width="150" />
      <el-table-column prop="status" label="是否完成" width="150" />
      <el-table-column prop="fertilizer" label="肥料类型" width="150" />
      <el-table-column prop="fertilizationcycle" label="施肥周期" width="150" />
      <el-table-column prop="growthcondition" label="生长状况" width="150" />
      <el-table-column prop="username" label="种植户" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{
            scope.row.username
          }}</span>
        </template></el-table-column
      >
      <el-table-column prop="phone" label="联系方式" width="150"
        ><template v-slot="scope">
          <span :class="{ 'disabled-cell': true }">{{ scope.row.phone }}</span>
        </template></el-table-column
      >
      <el-table-column fixed="right" label="操作" width="140">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            :icon="Edit"
            size="small"
            circle
            plain
            @click="onEditUserinfo(row, $index)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            circle
            plain
            @click="onDelVegetablePlotinfo(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <diaLog ref="dialog" @success="onSuccess"></diaLog>
  </page-container>
</template>
<style>
.disabled-cell {
  color: #ccc;
}
</style>
